package element;

import java.awt.Color;
import java.awt.Graphics2D;
import springiesSimulator.Assembly;


/**
 * @author Mark Govea
 */
public class Muscle extends Spring {
    private double myAmplitude;
    private double myPhase;

    /**
     * @param start first attached mass
     * @param end second attached mass
     * @param length default rest length of muscle
     * @param kVal spring constant
     * @param amplitude amount by which the default length varies
     */
    public Muscle (Mass start, Mass end, double length, double kVal,
            double amplitude) {
        super(start, end, length, kVal);
        setMyAmplitude(amplitude);
        setMyPhase(0);
    }

    @Override
    public void update (Assembly canvas, double dt) {
        setMyPhase((getMyPhase() + dt) % (2 * Math.PI));
        super.update(canvas, dt);
    }

    @Override
    public double getRestLength () {
        return getMyLength() + getMyAmplitude() * Math.sin(getMyPhase());
    }

    @Override
    public void setPaintColor (Graphics2D pen) {
        double len = getCurrentCompression();

        if (Math.abs(len) < REST_THRESHOLD) {
            pen.setColor(Color.GRAY);
        }
        else if (len > 0.0) {
            pen.setColor(Color.CYAN);
        }
        else {
            pen.setColor(Color.ORANGE);
        }
    }

    // Getters and Setters:

    /**
     * @return amplitude of muscle oscillation
     */
    public double getMyAmplitude () {
        return myAmplitude;
    }

    /**
     * @param amplitude sets amplitude of spring oscillation
     */
    protected void setMyAmplitude (double amplitude) {
        myAmplitude = amplitude;
    }

    /**
     * @return phase of muscle oscillation
     */
    public double getMyPhase () {
        return myPhase;
    }

    /**
     * @param phase sets phase of spring oscillation
     */
    protected void setMyPhase (double phase) {
        myPhase = phase;
    }
}
